package com.example.multiple.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.multiple.config.DynamicDataSource;
import com.example.multiple.config.MyDataSource;
import com.example.multiple.config.MyDataSourceConfig;
import com.example.multiple.model.SysUser;
import com.example.multiple.properties.AllDatasourceProperties;
import com.example.multiple.service.SysUserService;
import org.apache.ibatis.mapping.DatabaseIdProvider;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author yanggld
 * @Date 2019/6/19-11:32
 * @Description
 */
@RestController
public class SysUserController {

	@Autowired
	private AllDatasourceProperties allDatasourceProperties;

	@Autowired
	private SysUserService sysUserService;

	@GetMapping("/sys/user/{env}")
	public List<SysUser> list(@PathVariable String env){
		return sysUserService.list(env);
	}

	@Autowired
	private MyDataSourceConfig myDataSourceConfig;

	@GetMapping("/demo")
	public Object demo(){
		Map<Object, Object> targetDataSource = new HashMap<>();
		List<MyDataSource> myDataSourceList = myDataSourceConfig.getDs();
		for (MyDataSource ds : myDataSourceList) {
			targetDataSource.put(ds.getNickname(), ds);
		}
		DynamicDataSource.getInstance().setTargetDataSources(targetDataSource);
		return true;
	}

	@GetMapping("/sys/user/{env}/{id}")
	public SysUser selectById(@PathVariable String env,@PathVariable String id){
		System.out.println(allDatasourceProperties.getClients().length+"========================================================");
		System.out.println(JSONObject.toJSONString(allDatasourceProperties));
		return sysUserService.selectById(env,id);
	}
}
